--
-- Author: shunguo.chen
-- Date: 2018-09-27 10:41:34
--
-- ResRender 界面上方资源条

local ClassRef = app.mvc.createRenderClass(...)

function ClassRef.createWithData(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode("coinnum_node.csb"))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
	self.super.init(self, data)

    self._ui.bg:setTouchEnabled(true)
    self._ui.bg:addTouchEventListener(function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:showPanelTips()
        end
    end)

	self._ui.Button_plus:addClickEventListener(function()
        self:addRes()
        executeFunc(self._onClick)
	end)

    self._isNeedSetIcon = false
    self._btnVisible = true

	self:refresh()
end

function ClassRef:onClick(cb)
    self._onClick = cb
end

-- data = {type=Enums.TipsType.daibi/item, id=, num=}
function ClassRef:refresh()
    if self._data then
        local data = self._data
        self:setNum(data.num)

        -- 部分货币／抽奖券 有购买快捷按钮
        if data.id == Enums.lootType.coin or
            data.id == Enums.lootType.gold or 
            data.id == Enums.lootType.magicCrystal or
            data.id == Constant.Lottery_ItemId then

            self._ui.Button_plus:setVisible(true and self._btnVisible)
        else
            self._ui.Button_plus:setVisible(false)
        end

        if not self._isNeedSetIcon then return end

        local iconPath
        if data.type == Enums.TipsType.item then
            local conf = GD:queryItemById(data.id)
            iconPath = string.format("%s%s.png", Res.itemPath, conf.icon)
        elseif data.type == Enums.TipsType.daibi then
            iconPath = GD:getResIcon(data.id)
        end
        self:setIcon(iconPath)
    else
        self:setNum(0)
        self._ui.Button_plus:setVisible(false)
    end
end

function ClassRef:setIcon(icon)
	if not icon then return end
	self._ui.icon:loadTexture(icon)
end

function ClassRef:setNum(num)
	self._ui.num:setString(num or 0)
end

-- 直接根据UD数据刷新数量
function ClassRef:refreshNum()
    if not self._data then return end
    local data = self._data
    if data.type == Enums.TipsType.daibi then
        self:setNum(UD:getDaibiValue(data.id))
    elseif data.type == Enums.TipsType.item then
        self:setNum(UD:getItemAmountByTid(data.id))
    end
end

function ClassRef:isNeedSetIcon(isNeed)
    self._isNeedSetIcon =  isNeed
end

function ClassRef:setBtnVisible(b)
    self._btnVisible = b
end

function ClassRef:showPanelTips()
    if not self._data then return end
    local data = self._data
    local param = {itemId = data.id, tipsType = data.type}

    -- 玫瑰金币显示是货币，实际是道具，这里tips统一按道具处理
    -- 特殊的货币/道具
    if data.id == Enums.lootType.roseGold then
        param.itemId = Constant.RoseCoin_ItemId
        param.tipsType = Enums.TipsType.item
    end

    local boundingBoxTemp = self._ui.bg:getBoundingBox()
    local worldPosTemp = self._ui.bg:getParent():convertToWorldSpace(cc.p(boundingBoxTemp.x, boundingBoxTemp.y))
    boundingBoxTemp.x = worldPosTemp.x
    boundingBoxTemp.y = worldPosTemp.y
    param.boundingBox = boundingBoxTemp
    app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params = param})
end

function ClassRef:addRes()
    app:sendMsg("MallController", "buyGameRes", self._data.id)
end

return ClassRef